package com.example.myapp.login;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.myapp.R;
import com.example.myapp.fragment.HomeActivity;
import com.igexin.sdk.PushManager;
import com.tencent.connect.UserInfo;
import com.tencent.connect.auth.QQToken;
import com.tencent.connect.common.Constants;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class LoginActivity extends Activity {
	private static final String TAG = "LoginActivity";
	private static final String APP_ID = "1106207538";// 官方获取的APPID
	private Tencent mTencent;
	private BaseUiListener mIUiListener;
	private UserInfo mUserInfo;
	private CustomVideoView videoview;
	Button btn_login;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		PushManager.getInstance().initialize(this.getApplicationContext(),
				com.example.myapp.getui.DemoPushService.class);
		// com.getui.demo.DemoIntentService 为第三方自定义的推送服务事件接收类
		PushManager.getInstance().registerPushIntentService(this.getApplicationContext(),
				com.example.myapp.getui.DemoIntentService.class);
		// 传入参数APPID和全局Context上下文
		mTencent = Tencent.createInstance(APP_ID, LoginActivity.this.getApplicationContext());
		initVIew();
	}

	private void initVIew() {
		// TODO Auto-generated method stub
		btn_login = (Button) findViewById(R.id.btn_login);
		// 加载视频资源控件
		videoview = (CustomVideoView) findViewById(R.id.videoview);
		// 设置播放加载路径
		videoview.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video));
		// 播放
		videoview.start();
		// 循环播放
		videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
			@Override
			public void onCompletion(MediaPlayer mediaPlayer) {
				videoview.start();
			}
		});
		btn_login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 Intent intent=new Intent(LoginActivity.this,HomeActivity.class);
				 startActivity(intent);
				 finish();
				/**
				 * 通过这句代码，SDK实现了QQ的登录，这个方法有三个参数，第一个参数是context上下文，第二个参数SCOPO
				 * 是一个String类型的字符串，表示一些权限
				 * 官方文档中的说明：应用需要获得哪些API的权限，由“，”分隔。例如：SCOPE =
				 * “get_user_info,add_t”；所有权限用“all”
				 * 第三个参数，是一个事件监听器，IUiListener接口的实例，这里用的是该接口的实现类
				 */
//				mIUiListener = new BaseUiListener();
//				// all表示获取所有权限
//				mTencent.login(LoginActivity.this, "all", mIUiListener);
			}
		});
	}

	// 返回重启加载
	@Override
	protected void onRestart() {
		initVIew();
		super.onRestart();
	}

	// 防止锁屏或者切出的时候，音乐在播放
	@Override
	protected void onStop() {
		videoview.stopPlayback();
		super.onStop();
	}

	/**
	 * 自定义监听器实现IUiListener接口后，需要实现的3个方法 onComplete完成 onError错误 onCancel取消
	 */
	private class BaseUiListener implements IUiListener {

		@Override
		public void onComplete(Object response) {
//			Toast.makeText(LoginActivity.this, "授权成功", Toast.LENGTH_SHORT).show();
			Log.e(TAG, "response:" + response);
			JSONObject obj = (JSONObject) response;
			try {
				String openID = obj.getString("openid");
				String accessToken = obj.getString("access_token");
				String expires = obj.getString("expires_in");
				mTencent.setOpenId(openID);
				mTencent.setAccessToken(accessToken, expires);
				QQToken qqToken = mTencent.getQQToken();
				mUserInfo = new UserInfo(getApplicationContext(), qqToken);
				mUserInfo.getUserInfo(new IUiListener() {
					@Override
					public void onComplete(Object response) {
						Log.e(TAG, "登录成功" + response.toString());
						System.out.println("登录成功--body-->>"+response.toString());
						 Intent intent=new Intent(LoginActivity.this,HomeActivity.class);
						 Bundle bundle=new Bundle();
						 bundle.putString("info", response.toString());
						 intent.putExtras(bundle);
						 startActivity(intent);
						 finish();
					}

					@Override
					public void onError(UiError uiError) {
						Log.e(TAG, "登录失败" + uiError.toString());
						System.out.println("登录失败--body-->>"+uiError.toString());
					}

					@Override
					public void onCancel() {
						Log.e(TAG, "登录取消");
						System.out.println("登录取消--body-->>");
					}
				});
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		@Override
		public void onError(UiError uiError) {
			Toast.makeText(LoginActivity.this, "授权失败", Toast.LENGTH_SHORT).show();

		}

		@Override
		public void onCancel() {
			Toast.makeText(LoginActivity.this, "授权取消", Toast.LENGTH_SHORT).show();
		}

	}
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == Constants.REQUEST_LOGIN){
            Tencent.onActivityResultData(requestCode,resultCode,data,mIUiListener);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
